home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / htmlsample / aboutbox.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.5 KB  |  177 lines

  1. /*
  2.     file AboutBox.c
  3.     
  4.     Description:
  5.     This file contains the routines used to manage the about box window
  6.     displayed when the user chooses 'About HTMLSample...' from the file menu.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.     by John Montbriand, 1999.
  14.  
  15.     Copyright: © 1999 by Apple Computer, Inc.
  16.     all rights reserved.
  17.     
  18.     Disclaimer:
  19.     You may incorporate this sample code into your applications without
  20.     restriction, though the sample code has been provided "AS IS" and the
  21.     responsibility for its operation is 100% yours.  However, what you are
  22.     not permitted to do is to redistribute the source as "DSC Sample Code"
  23.     after having made changes. If you're going to re-distribute the source,
  24.     we require that you make it clear in the source that the code was
  25.     descended from Apple Sample Code, but that you've made changes.
  26.     
  27.     Change History (most recent first):
  28.     10/16/99 created by John Montbriand
  29. */
  30.  
  31. #include "AboutBox.h"
  32.  
  33. #include <HTMLRendering.h>
  34. #include <Resources.h>
  35. #include <StdDef.h>
  36. #include <string.h>
  37. #include <Sound.h>
  38.  
  39.  
  40. enum {
  41.     kAboutBoxWindowID = 129, /* WIND resource ID for the about box. */
  42.     kMaxAboutBoxHeight = 380 /* maximum about box window height */
  43. };
  44.  
  45.     /* resource type and ID for the about box's contents */
  46. enum {
  47.     kHTMLResourceType = 'HTML',
  48.     kAboutBoxHTMLID = 128
  49. };
  50.  
  51.     /* variables used by the about box.  there is only
  52.         one about box, so these are allocated as globals */
  53. WindowPtr gAboutBox = NULL;
  54. HRReference gAboutRenderer = NULL;
  55.  
  56.  
  57. /* IsAboutBox returns true if the window pointer
  58.     in the aboutBox parameter is not NULL and
  59.     points to the about box. */
  60. Boolean IsAboutBox(WindowPtr aboutBox) {
  61.     return (aboutBox == gAboutBox && aboutBox != NULL);
  62. }
  63.  
  64.  
  65. /* OpenAboutBox opens the about box window and returns
  66.     a pointer to the window in *aboutBox.  There can only
  67.     be one about box open at a time, so if the about box is
  68.     already open, then this routine brings it to the front
  69.     by calling SelectWindow before returning a pointer to
  70.     it. */
  71. OSStatus OpenAboutBox(WindowPtr *aboutBox) {
  72.     OSStatus err;
  73.     if (gAboutBox != NULL) {
  74.             /* already showing??? bring it to the front. */
  75.         SelectWindow(gAboutBox);
  76.     } else {
  77.         Point theSize;
  78.         Handle text;
  79.         gAboutBox = NULL;
  80.         gAboutRenderer = NULL;
  81.             /* get the window */
  82.         gAboutBox = GetNewCWindow(kAboutBoxWindowID, NULL, (WindowPtr)(-1));
  83.         if (gAboutBox == NULL) { err = resNotFound; goto bail; }
  84.             /* allocate a new rendering object */
  85.         err = HRNewReference(&gAboutRenderer, kHRRendererHTML32Type, gAboutBox);
  86.         if (err != noErr) goto bail;
  87.             /* we don't have a grow box in this window */
  88.         err = HRSetGrowboxCutout(gAboutRenderer, false);
  89.         if (err != noErr) goto bail;
  90.             /* we only want a vertical scroll bar */
  91.         err = HRSetScrollbarState(gAboutRenderer, eHRScrollbarOff, eHRScrollbarAuto);
  92.         if (err != noErr) goto bail;
  93.             /* set the bounds for the rendering object */
  94.         SetPort(gAboutBox);
  95.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  96.         if (err != noErr) goto bail;
  97.             /* get the HTML data we want to draw in the window */
  98.         text = GetResource(kHTMLResourceType, kAboutBoxHTMLID);
  99.         if (text == NULL) { err = resNotFound; goto bail; }
  100.             /* put the data into the rendering object */
  101.         MoveHHi(text);
  102.         HLock(text);
  103.         err = HRGoToPtr(gAboutRenderer, *text, GetHandleSize(text), false, false);
  104.         HUnlock(text);
  105.         if (err != noErr) goto bail;
  106.             /* find out the 'best' rectangle for displaying the data */
  107.         err = HRGetRenderedImageSize(gAboutRenderer, &theSize);
  108.         if (err != noErr) goto bail;
  109.             /* adjust the window's size, constraining it by the
  110.             maximum size */
  111.         if (theSize.v > kMaxAboutBoxHeight) theSize.v = kMaxAboutBoxHeight;
  112.         SizeWindow(gAboutBox, theSize.h+16, theSize.v, false);
  113.         SetPort(gAboutBox);
  114.             /* adjust the rendering object's size to the window's size */
  115.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  116.         if (err != noErr) goto bail;
  117.             /* show the window */
  118.         ShowWindow(gAboutBox);
  119.     }
  120.         /* done, return the window pointer */
  121.     *aboutBox = gAboutBox;
  122.     return noErr;
  123. bail:
  124.     if (gAboutRenderer != NULL) { HRDisposeReference(gAboutRenderer); gAboutRenderer = NULL; }
  125.     if (gAboutBox) { DisposeWindow(gAboutBox); gAboutBox = NULL; }
  126.     return err;
  127. }
  128.  
  129.  
  130. /* AboutBoxCloseWindow closes the about box window. 
  131.     this routine deallocates any structures allocated
  132.     by the OpenAboutBox. */
  133. void AboutBoxCloseWindow(WindowPtr aboutBox) {
  134.     if (IsAboutBox(aboutBox)) { 
  135.         HRDisposeReference(gAboutRenderer);
  136.         gAboutRenderer = NULL;
  137.         DisposeWindow(gAboutBox);
  138.         gAboutBox = NULL;
  139.     }
  140. }
  141.  
  142.  
  143. /* EnsureAboutBoxIsClosed closes the about box window
  144.     if it is open.  If it is not open then this routine does
  145.     nothing. */
  146. void EnsureAboutBoxIsClosed(void) {
  147.     if (gAboutBox != NULL) 
  148.         AboutBoxCloseWindow(gAboutBox);
  149. }
  150.  
  151.  
  152. /* AboutBoxUpdate should be called for update events
  153.     directed at the about box window.  It calls
  154.     BeginUpdate and EndUpdate and does all of the
  155.     drawing required to refresh the about box window. */
  156. void AboutBoxUpdate(WindowPtr aboutBox) {
  157.     if (IsAboutBox(aboutBox)) {
  158.         SetPort(gAboutBox);
  159.         BeginUpdate(gAboutBox);
  160.         HRDraw(gAboutRenderer, gAboutBox->visRgn);
  161.         EndUpdate(gAboutBox);
  162.     }
  163. }
  164.  
  165.  
  166. /* AboutBoxActivate should be called for activate events
  167.     directed at the about box window. */
  168. void AboutBoxActivate(WindowPtr aboutBox, Boolean activate) {
  169.     if (IsAboutBox(aboutBox)) {
  170.         SetPort(gAboutBox);
  171.         if (activate)
  172.             HRActivate(gAboutRenderer);
  173.         else HRDeactivate(gAboutRenderer);
  174.     }
  175. }
  176.  
  177.